package com.googlecode.worktools.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;

import com.googlecode.worktools.domain.FunctionInfo;
import com.googlecode.worktools.ui.BaseFunctionPanel;

/**
 * 功能点操作相关工具类
 * @author Kobe Su,2009-12-1
 */
public class FunctionUtils {
	
	private static final String XML_PATH = "/rescources/functionDatas.xml";
	private static List<FunctionInfo> functionDatas = new ArrayList<FunctionInfo>();
	private static Map<String,BaseFunctionPanel> functionPanelMap = new HashMap<String,BaseFunctionPanel>();
	
	private FunctionUtils() {};
	
	
	static {
		loadFunctionDatas();
	}
	
	public static void loadFunctionDatas() {
		Document doc = Dom4jUtils.readXml(FunctionUtils.class.getResourceAsStream(XML_PATH));
		List list = doc.selectNodes("/functions/function");
		for(int i=0;i<list.size();i++) {
			Element element = (Element)list.get(i);
			FunctionInfo function = new FunctionInfo();
			function.setFunctionId(element.attributeValue("id"));
			function.setFunctionName(element.attributeValue("name"));
			function.setFunctionPanelClass(element.attributeValue("class"));
			functionDatas.add(function);
		}

	}

	public static List<FunctionInfo> getFunctionDatas() {
		return functionDatas;
	}

	public static BaseFunctionPanel getFunctionPanel(String functionId) {
		return functionPanelMap.get(functionId);
	}

	public static void addFunctionPanel(String functionId,BaseFunctionPanel functionPanel) {
		functionPanelMap.put(functionId, functionPanel);
	}
	
	public static void main(String[] args) {
		for(FunctionInfo function : functionDatas) {
			System.out.println("id:"+function.getFunctionId()+"  name:"+function.getFunctionName());
		}

	}

}
